
function backToTopTab() {
	var a = "div.tabs/a.act".resolveFirst($("content"));
	a.fireEvent("onclick");
}

function fixAltA() {
	if (navigator.userAgent.indexOf('Firefox/2') > 0)
		$('alta').innerHTML = 'Alt+Shift+A';
	else
		$('alta').innerHTML = 'Alt+A';
}

var CbWidget ={
	selectAll: function(id, size){
		var chked = $(id).checked;
		for(var i=0; i<size; i++){
			var el = $(id + '_' + i);
			if(el && !el.disabled){
				el.checked = chked;
			}
		}
	}
}

/*
 * C2ID combo box
 */
var C2idSelect = {
	
	onPrjChange: function(event, noOnChange) {
		var prj = event.target || event.srcElement;
		var name = prj.id.substring(0, prj.id.length - "_prj".length);
		var c2id = $(name);
		var modiu = $(name + "_modiu");
		
		var prjValue = prj.options[prj.selectedIndex].value;
		
		if (prjValue == "ALL" || prjValue == "") {
			// ALL is selected, disable modiu select
			modiu.selectedIndex = 0;
			modiu.disabled = "true";
		} else {
			// a project is selected, update modiu select
			while (modiu.options.length > 1)
				modiu.remove(1);
			var modius = c2id.prj2modius[prjValue];
			for (var i = 0; i < modius.length; i++)
				modiu.options.add(new Option(modius[i], modius[i]));
			
			modiu.selectedIndex = 0;
			modiu.disabled = "";
		}
		
		c2id.value = prjValue;
		
		if (noOnChange) return;
		c2id.fireEvent('onchange');
	},
	
	onModIuChange: function(event, noOnChange) {
		var modiu = event.target || event.srcElement;
		var name = modiu.id.substring(0, modiu.id.length - "_modiu".length);
		var c2id = $(name);
		var prj = $(name + "_prj");

		var prjValue = prj.options[prj.selectedIndex].value;
		var modiuValue = modiu.options[modiu.selectedIndex].value;
		c2id.value = prjValue + modiuValue;

		if (noOnChange) return;
		c2id.fireEvent('onchange');
	},
	
	init: function(name, dft1, dft2) {
		var prj = $(name + "_prj");
		var modiu = $(name + "_modiu");
		Event.observe(prj, "change", C2idSelect.onPrjChange);
		Event.observe(modiu, "change", C2idSelect.onModIuChange);
		
		if (dft1) {
			Yutil.selectByValue(prj, dft1);
			C2idSelect.onPrjChange({target: prj}, "noOnChange");
			if (dft2) {
				Yutil.selectByValue(modiu, dft2);
				C2idSelect.onModIuChange({target: modiu}, "noOnChange");
			}
		}

		var c2id = $(name);
		var prjValue = prj.options[prj.selectedIndex].value;
		var modiuValue = modiu.options[modiu.selectedIndex].value;
		c2id.value = prjValue + modiuValue;
	}
}



/*
 * Report customization
 */
function tableSelected(ee){
	for(var i=0; i<ee.options.length; i++){
		if(ee.options[i].selected){
			 $('table' + i).style.display = 'block' ;
		}else{
		$('table' + i).style.display = 'none' ;
		}
	}

}
function moveOption(e1, e2, x){
	try{
		for(var i=0; i<e1.options.length; i++){
			if(e1.options[i].selected){
				var e = e1.options[i];
				e2.options.add(new Option(e.text, e.value));
				e1.remove(i);
				i=i-1
			}
		}

	$('columns' + x).value=getvalue($('list2' + x));
	}
	catch(e){}
}
function getvalue(geto){
	var allvalue = "";
	for(var i=0; i<geto.options.length; i++){
		allvalue +=geto.options[i].value + ",";
	}
	return allvalue;
}
function changepos(obj,index, x){
	if(index==-1){
		if (obj.selectedIndex>0){
			//since swapNode is not supported in FireFox, so use an other method swapNodes();
			//obj.options[obj.selectedIndex].swapNode(obj.options[obj.selectedIndex-1])
			swapNodes(obj.options[obj.selectedIndex],obj.options[obj.selectedIndex-1] );
		}
	}
	else if(index==1){
			if (obj.selectedIndex<obj.options.length-1){
				//obj.options[obj.selectedIndex].swapNode(obj.options[obj.selectedIndex+1])
				swapNodes(obj.options[obj.selectedIndex], obj.options[obj.selectedIndex+1]);
			}
	}

	$('columns' + x).value=getvalue($('list2' + x));
}
function swapNodes(item1,item2) {
	// We need a clone of the node we want to swap
	var itemtmp = item1.cloneNode(1);

	// We also need the parentNode of the items we are going to swap.
	var parent = item1.parentNode;

	// First replace the second node with the copy of the first node
	// which returns a the new node
	item2 = parent.replaceChild(itemtmp,item2);

	//Then we need to replace the first node with the new second node
	parent.replaceChild(item2,item1);

	// And finally replace the first item with it's copy so that we
	// still use the old nodes but in the new order. This is the reason
	// we don't need to update our Behaviours since we still have 
	// the same nodes.
	parent.replaceChild(item1,itemtmp);

	// Free up some memory, we don't want unused nodes in our document.
	itemtmp = null;
}

function toJsonString(json) {
	if(json == {}) return '{}';
	var type = typeof(json);
    switch (type) {
      case 'undefined':
      case 'function':
      case 'unknown': return;
      case 'boolean': return json.toString();
    }
    if(json instanceof String || typeof(json) == 'string' || typeof(json) == 'number')
		return json;
	var str = new StringBuffer;
	if(json != null && typeof(json) == 'object' && 'splice' in json && 'join' in json) {
		if(json.length == 0) return '[]';
		return '[' + json.join(',') + ']';
	} 
	var pushed = [];
	var i = 0;
	for(var p in json) {
		var value = toJsonString(json[p]);
		pushed.push(p + ':' + value);
	}
	return '{' + pushed.join(',') + '}';
}

